﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using T.ApiResults;
using T.Settlement.API.Application.Command.PaymentCommand;

namespace T.Settlement.API.Controllers
{
    /// <summary>
    /// 付款控制器(写)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class PaymentController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="mediator">中介者</param>
        public PaymentController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加付款方式
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Create(PaymentCreateCommand command)
        {
            ApiResult<int> result=new ApiResult<int>();
            try
            {
                result.Data=await _mediator.Send(new PaymentCreateCommand
                {
                    PaymentId = command.PaymentId,
                    PaymentTitle = command.PaymentTitle,
                    Description = command.Description,
                    PaymentPrice = command.PaymentPrice,
                    BillingMethod = command.BillingMethod,
                    PaymentMethods = command.PaymentMethods,
                    OtherBillingMethod = command.OtherBillingMethod,
                    PaymentBank = command.PaymentBank,
                    BankAccount = command.BankAccount,
                    PaymentApplicant = command.PaymentApplicant,
                    PaymentDate = command.PaymentDate,
                    PaymentRemark = command.PaymentRemark,
                    TicketAttachments = command.TicketAttachments,
                    ApprovalStatus = command.ApprovalStatus,
                    Approver = command.Approver,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取用户名称
                    CreatedDate = DateTime.Now,
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改付款方式
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Update(PaymentUpdCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new PaymentUpdCommand
                {
                    PaymentId = command.PaymentId,
                    PaymentTitle = command.PaymentTitle,
                    Description = command.Description,
                    PaymentPrice = command.PaymentPrice,
                    BillingMethod = command.BillingMethod,
                    PaymentMethods = command.PaymentMethods,
                    OtherBillingMethod = command.OtherBillingMethod,
                    PaymentBank = command.PaymentBank,
                    BankAccount = command.BankAccount,
                    PaymentApplicant = command.PaymentApplicant,
                    PaymentDate = command.PaymentDate,
                    PaymentRemark = command.PaymentRemark,
                    TicketAttachments = command.TicketAttachments,
                    ApprovalStatus = command.ApprovalStatus,
                    Approver = command.Approver,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取用户名称
                    UpdatedDate = DateTime.Now,
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除付款方式
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Delete(PaymentDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new PaymentDelCommand
                {
                    PaymentId = command.PaymentId,
                    PaymentTitle = command.PaymentTitle,
                    Description = command.Description,
                    PaymentPrice = command.PaymentPrice,
                    BillingMethod = command.BillingMethod,
                    PaymentMethods = command.PaymentMethods,
                    OtherBillingMethod = command.OtherBillingMethod,
                    PaymentBank = command.PaymentBank,
                    BankAccount = command.BankAccount,
                    PaymentApplicant = command.PaymentApplicant,
                    PaymentDate = command.PaymentDate,
                    PaymentRemark = command.PaymentRemark,
                    TicketAttachments = command.TicketAttachments,
                    ApprovalStatus = command.ApprovalStatus,
                    Approver = command.Approver,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取用户名称
                    DeletedDate = DateTime.Now,
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 批删付款方式
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> BatchDel([FromQuery]PaymentBatchDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改审批状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdStatus([FromQuery]PaymentUpdStatusCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 添加审批
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ApprovalCreate(PaymentApprovalCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new PaymentApprovalCreateCommand
                {
                    PaymentId = command.PaymentId,
                    ApprovalRecordId = command.ApprovalRecordId,
                    ApprovalResult = command.ApprovalResult,
                    ApprovalNotes = command.ApprovalNotes,
                    Subgrantor = command.Subgrantor,
                    Approver = command.Approver,
                    ContractId = command.ContractId,
                    MaterialStoreId = command.MaterialStoreId,
                    MaterialRequisitionId = command.MaterialRequisitionId,
                    PersonnelId = command.PersonnelId,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取用户名称
                    CreatedDate=DateTime.Now,
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public IActionResult UploadFiles(IFormFile file)
        {
            try
            {
                string savePth = Directory.GetCurrentDirectory() + @"\wwwroot\Pic\";
                string NewFileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
                using (FileStream fs = new FileStream(savePth + NewFileName, FileMode.Create))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                return Ok(NewFileName);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
